#pragma once
#include <string>
#include <fstream>

class Util
{
public:
    static int ReadFileContent(const std::string &filepath, std::string &out)
    {
        // version1 --- 仅纯文本
        // std::ifstream ifs(filepath);
        // if (!ifs.is_open())
        //     return false;
        // std::string line;
        // while (std::getline(ifs, line))
        // {
        //     out += line;
        // }
        // ifs.close();
        // return true;

        std::ifstream in(filepath, std::ios::binary);
        if (!in.is_open())
            return -1;
        in.seekg(0, in.end);
        int filesize = in.tellg();
        in.seekg(0, in.beg);

        std::string content;
        content.resize(filesize);
        in.read((char *)content.c_str(), content.size());

        out = content;
        in.close();
        return filesize;
    }

    static std::string ReadOneLine(std::string &data, std::string &out, const std::string glinespace)
    {
        size_t pos = data.find(glinespace);
        if (pos == std::string::npos)
            return std::string();
        out = data.substr(0, pos);
        data.erase(0, pos + glinespace.size());
        return out;
    }
};